﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Exhibition.RenderTemplete.Controls;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;
using Exhibition.Service;

namespace Exhibition.Manage
{
    public partial class AdminUserDetail : BaseAdminPage
    {
        AdminUserModel model = new AdminUserModel();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                hfId.Value = reqID.ToString();
                BindDrop();
                GetAdminUser();

            }
        }

        private void BindDrop()
        {
            List<RoleModel> mdls=CenterService.roleService.GetAll();

            StringUtils.BindDropdownList(drpRole, "RoleName", "Id", mdls);
        }

        private void GetAdminUser()
        {
            model = CenterService.adminUserService.GetOneById(reqID);
            if (model != null)
            {

                //用户名
                txtUserName.Value = model.UserName.ToString();
                if (model.UserName == "admin") {
                    drpRole.Enabled = false;
                }
                txtUserName.Disabled = true;
                //密码
                txtPassWord.Value = model.PassWord.ToString();

                //真实姓名
                txtRealName.Value = model.RealName.ToString();



                //
                rdoUsed.SelectedValue = model.Status.ToString();

                //
                drpRole.SelectedValue = model.RoleId.ToString();

                //
                // txtFuncs.Value = model.Funcs.ToString();

            }
        }



        /// <summary>
        /// 保存一条数据
        /// <summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {

            model = string.IsNullOrEmpty(hfId.Value)||hfId.Value=="0" ? new AdminUserModel() : CenterService.adminUserService.GetOneById(Convert.ToInt32(hfId.Value));
            //用户名
            model.UserName = txtUserName.Value;

            //密码
            if (!string.IsNullOrEmpty(txtPassWord.Value))
            {
                model.PassWord = StringUtils.EncryptPassWord(txtPassWord.Value, StringUtils.PassFormat.MD5);
            }
            //真实姓名
            model.RealName = txtRealName.Value;
            model.CreateUserId = model.CreateUserId == 0 ? AdminUser.Id : model.CreateUserId;
            //
            model.Status = Convert.ToInt32(rdoUsed.SelectedValue );

            //
            model.RoleId = Convert.ToInt32(drpRole.SelectedValue);


            if (CenterService.adminUserService.Save(model) > 0)
            {

                JsHelper.Unblock(this, CommFunMsg.GetMessage("A0001", Lan.zh));
            }
            else
            {
                JsHelper.Alert(this, CommFunMsg.GetMessage("E0001", Lan.zh));
            }

        }

    }

}